package com.neiquan.weiguan.fragment.base;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neiquan.weiguan.R;
import com.neiquan.weiguan.adapter.MyBaseAdapter;
import com.topdrag.view.HeaderViewPagerLayout;

import net.neiquan.applibrary.wight.RefreshLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者: hjb
 * 时间：2016/3/4.
 */
public abstract class BaseListFragment extends BaseFragment implements RefreshLayout.RetryListener, HeaderViewPagerLayout.ScrollableContainer {

    public int pageNum = 0;
    public final int PAEG_SIZE = 10;
    public View view;
    public MyBaseAdapter adapter;
    @InjectView(R.id.recyclerview)
    public XRecyclerView mRecyclerview;
    /*  @InjectView(R.id.refesh_ly)
      RefreshLayout mRefeshLy;*/
    @InjectView(R.id.head_view)
    RelativeLayout mHeadView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.base_common_xlistview, null);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHeadView(isHaveHead());
        setHeadViews();
        adapter = getAdapter();
        mRecyclerview.setAdapter(adapter);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getLineNum(),
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerview.setArrowImageView(R.drawable.iconfont_downgrey);
//        if (getLineNum() == 1) {
//            mRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
//        } else {
//            mRecyclerview.addItemDecoration(new DividerGridItemDecoration(getActivity()));
//        }
        mRecyclerview.setLayoutManager(layoutManager);
        //  mRefeshLy.setRetryListener(this);
        getDateFromWeb();

    }

    protected abstract boolean isHaveHead();

    /**
     * 当返回为true时候设置
     */
    protected abstract void setHeadViews();

    private void setHeadView(boolean isHaveHead) {
        if (!isHaveHead) {
            mHeadView.setVisibility(View.GONE);
        } else {
            mHeadView.setVisibility(View.VISIBLE);
        }
    }


    protected abstract MyBaseAdapter getAdapter();

    protected abstract int getLineNum();

    protected abstract void getDateFromWeb();


    protected void setTitleTv(int id) {
        TextView tv = (TextView) view.findViewById(R.id.title_tv);
        tv.setText(id);
    }

    protected void setTitleTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.title_tv);
        tv.setText(str);
    }

    protected void setBackTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.back_tv);
        tv.setText(str);
    }

    protected void setNextTv(String str) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = view.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    protected TextView getNextTv() {

        return (TextView) view.findViewById(R.id.next_tv);
    }

    protected void setBackGone() {
        View tv = view.findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }

    protected void setBackOnClick(View.OnClickListener onClick) {
        LinearLayout ly_back = (LinearLayout) view.findViewById(R.id.ly_back);
        ly_back.setOnClickListener(onClick);
        View iv = view.findViewById(R.id.back_img);
        iv.setOnClickListener(onClick);
    }


    @SuppressLint("NewApi")
    protected void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    protected void setNextImage(int id) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) view.findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    protected void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = view.findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    protected void setNextGone() {
        TextView tv = (TextView) view.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) view.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    @Override
    public View initRootView() {
        return null;
    }

    @Override
    public void setViews() {

    }

    @Override
    public void initData() {

    }
}
